package com.FH.organization.work_schedule;

import com.FH.organization.Attachment.AttachmentResource;
import com.FH.organization.department.DepartmentResource;
import com.FH.organization.operator_log.OperatorLogResource;
import com.FH.organization.people.KamMemberResource;
import com.FH.organization.suggestion.SuggestionResource;
import com.FH.organization.work_flow.WorkFlowResource;
import com.FH.util.AccessDB;
import com.FH.util.ExportExcel;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/12/16.
 */
@Stateless
public class WorkScheduleImpl extends AccessDB implements WorkScheduleDTO {
    @Inject
    KamMemberResource kamMemberResource;
    @Inject
    OperatorLogResource operatorLogResource;
    @Inject
    DepartmentResource departmentResource;
    @Inject
    WorkFlowResource workFlowResource;
    @Inject
    AttachmentResource attachmentResource;
    @Inject
    SuggestionResource suggestionResource;

    @Override
    public ResultDTO getWorkScheduleInfo(String serach, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(serach);
            String existName = "0";

            String sqlexit = "select count(1) from work_schedule where 1=1 ";

            String sql_w = "";//查询条件sql组合
            String sql_page = "";//分页组合

            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("member_id")) {
                sql_w += " AND member_id = '" + json.getString("member_id") + "'";
            }
            if (json.has("work_schedule_id")) {
                sql_w += " AND work_schedule_id = '" + json.getString("work_schedule_id") + "'";
            }
            if (json.has("work_schedule_time")) {
                sql_w += " AND work_schedule_time = '" + json.getString("work_schedule_time") + "'";
            }

            String sql_ok = sqlexit + sql_w + sql_page;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<WorkScheduleShowDTO> result = new ArrayList<WorkScheduleShowDTO>();

            if (!existName.equals("0")) {
                String sqlq = "select work_schedule_id,work_schedule_time,work_schedule_section,work_schedule_timestart,work_schedule_timeend,member_id FROM work_schedule where 1=1  " + sql_w + " ORDER BY work_schedule_timestart ASC" + sql_page;

                seresult = this.getReportingTo(sqlq);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    WorkScheduleShowDTO workScheduleShowDTO = new WorkScheduleShowDTO();
                    workScheduleShowDTO.setWork_schedule_id(seresult.get(i)[1]);

                    workScheduleShowDTO.setWork_schedule_time(seresult.get(i)[2]);
                    workScheduleShowDTO.setWork_schedule_section(seresult.get(i)[3]);
                    workScheduleShowDTO.setWork_schedule_timestart(seresult.get(i)[4]);
                    workScheduleShowDTO.setWork_schedule_timeend(seresult.get(i)[5]);
                    workScheduleShowDTO.setMember_id(seresult.get(i)[6]);
                    result.add(workScheduleShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();

        }
        return resultDTO;
    }

    @Override
    public ResultDTO addWorkScheduleInfo(WorkSchedulePullDTO workSchedulePullDTO) {
        ResultDTO resultDTO = new ResultDTO();//构建返回对象


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String existName = "";
        String work_schedule_id = workSchedulePullDTO.getWork_schedule_id();
        if (work_schedule_id != null && !work_schedule_id.equals("")) {

            String sql = "select count(1) from work_schedule where work_schedule_id='" + work_schedule_id + "'";

            existName = getReportingToOne(sql);

            if (!existName.equals("0")) {
                return updateWorkScheduleInfo(workSchedulePullDTO);
            }
        }

        String QUERY = "";

        //表中查看时间设为空值
        QUERY += "insert into work_schedule(work_schedule_id,work_schedule_time,work_schedule_section,work_schedule_timestart,work_schedule_timeend,member_id,create_time) values" +
                " ('" + uuid + "','" + workSchedulePullDTO.getWork_schedule_time() + "','" + workSchedulePullDTO.getWork_schedule_section() + "','" + workSchedulePullDTO.getWork_schedule_timestart()
                + "','" + workSchedulePullDTO.getWork_schedule_timeend() + "','" + workSchedulePullDTO.getMember_id() + "','" + time + "')";
        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStr(uuid);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }


    @Override
    public ResultDTO updateWorkScheduleInfo(WorkSchedulePullDTO workSchedulePullDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        //更新sql语句
        QUERY += "update work_schedule set update_time ='" + time + "'";

        if (null != workSchedulePullDTO.getWork_schedule_time() && !"".equals(workSchedulePullDTO.getWork_schedule_time())) {
            QUERY += ",work_schedule_time ='" + workSchedulePullDTO.getWork_schedule_time() + "'";
        }
        if (null != workSchedulePullDTO.getWork_schedule_section() && !"".equals(workSchedulePullDTO.getWork_schedule_section())) {
            QUERY += ",work_schedule_section ='" + workSchedulePullDTO.getWork_schedule_section() + "'";
        }
        if (null != workSchedulePullDTO.getWork_schedule_timestart() && !"".equals(workSchedulePullDTO.getWork_schedule_timestart())) {
            QUERY += ",work_schedule_timestart ='" + workSchedulePullDTO.getWork_schedule_timestart() + "'";
        }
        if (null != workSchedulePullDTO.getWork_schedule_timeend() && !"".equals(workSchedulePullDTO.getWork_schedule_timeend())) {
            QUERY += ",work_schedule_timeend ='" + workSchedulePullDTO.getWork_schedule_timeend() + "'";
        }

        QUERY += " where work_schedule_id = '" + workSchedulePullDTO.getWork_schedule_id() + "'";

        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStr(workSchedulePullDTO.getWork_schedule_id());
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");
        }
        return resultDTO;
    }

    @Override
    public ResultDTO deleteWorkScheduleInfo(WorkSchedulePullDTO workSchedulePullDTO) {
        //删除之前查询是否有记录
        String sql = "select count(1) from work_schedule where work_schedule_id ='" + workSchedulePullDTO.getWork_schedule_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {

            String QUERY = "";
            QUERY += "delete from work_schedule where work_schedule_id='" + workSchedulePullDTO.getWork_schedule_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO exportSchedule() {

        ResultDTO resultDTO = new ResultDTO();

        String downloadUrl = "";

        String sql = "SELECT CASE ws.work_schedule_time WHEN '1' THEN '上午' WHEN '2' THEN '下午' WHEN '3' THEN  '晚上' END as work_schedule_time " +
                "  ,ws.work_schedule_section,ws.work_schedule_timestart,ws.work_schedule_timeend FROM work_schedule ws " +
                "WHERE 1=1 GROUP BY ws.member_id,ws.work_schedule_time,work_schedule_timestart";

        List<String[]> seresult = new ArrayList<String[]>();

        seresult = this.getReportingTo(sql);

        List<ExportScheduleDTO> result = new ArrayList<ExportScheduleDTO>();

        for(int i=0;i<seresult.size();i++){
            ExportScheduleDTO exportScheduleDTO = new ExportScheduleDTO();
            exportScheduleDTO.setWork_schedule_time(seresult.get(i)[1] == null?"":seresult.get(i)[1]);
            exportScheduleDTO.setWork_schedule_section(seresult.get(i)[2] == null?"":seresult.get(i)[2]);
            exportScheduleDTO.setWork_schedule_timestart(seresult.get(i)[3] == null?"":seresult.get(i)[3]);
            exportScheduleDTO.setWork_schedule_timeend(seresult.get(i)[4] == null?"":seresult.get(i)[4]);

            result.add(exportScheduleDTO);
        }



        String[] headers = {"时间", "节次", "开始时间", "结束时间" };
        String title = "作息时间表";
        String filename = "作息时间表";
        ExportExcel<ExportScheduleDTO> exportExcel = new ExportExcel<ExportScheduleDTO>();
        downloadUrl = exportExcel.createExcelFile(exportExcel, title, headers, result, filename);

        if(result.size() > 0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.get_success);
            resultDTO.setStr(downloadUrl);
        }
        else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.get_fail);
        }

        return resultDTO;
    }
}
